﻿using System;
using System.Linq;
using System.Web.Mvc;
using SusuCMS.Web.Areas.Admin.Models;
using System.Collections.Generic;
using SusuCMS.Caching;

namespace SusuCMS.Web.Areas.Admin.Controllers
{
    [Authorize]
    public class WebsiteController : AdminControllerBase
    {
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var action = filterContext.ActionDescriptor.ActionName.ToLower();
            var attributes = filterContext.ActionDescriptor.GetCustomAttributes(typeof(HttpPostAttribute), false);
            var isPost = attributes.Count() > 0;

            if ((isPost && (action == "edit" || action == "create" || action == "delete")) || action == "toggleonline")
            {
                // Update Cache
                CmsDataCache.UpdateOnlineWebsites();
                CmsDataCache.UpdateOnlineHosts();
            }
        }

        public ActionResult Index()
        {
            return View(WebsiteRepository.FindAll());
        }

        #region Create

        public ActionResult Create()
        {
            return View(new Website());
        }

        [HttpPost]
        public ActionResult Create(Website model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    model.CreateTime = DateTime.Now;
                    WebsiteRepository.Add(model);
                    UnitOfWork.Save();

                    TempData["Message"] = "Create Website Successfully";

                    return RedirectToIndex();
                }
                catch
                {
                    TempData["ErrorMessage"] = "Create Website Failed";
                }
            }

            return View(model);
        }

        #endregion

        #region Edit

        public ActionResult Edit(int id)
        {
            var website = WebsiteRepository.FindAll().FirstOrDefault(i => i.Id == id);
            if (website == null)
            {
                return HttpNotFound();
            }

            return View(website);
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var website = WebsiteRepository.FindAll().FirstOrDefault(i => i.Id == id);
            UpdateModel(website);

            if (ModelState.IsValid)
            {
                try
                {
                    UnitOfWork.Save();

                    TempData["Message"] = "Edit Website Successfully";

                    return RedirectToIndex();
                }
                catch
                {
                    TempData["ErrorMessage"] = "Edit Website Failed";
                }
            }

            return View(website);
        }

        #endregion

        #region Delete

        [HttpPost]
        public ActionResult Delete(int id)
        {
            var website = WebsiteRepository.FindAll().FirstOrDefault(i => i.Id == id);
            if (website == null)
            {
                TempData["ErrorMessage"] = "Website Not Exist";
            }
            else
            {
                try
                {
                    WebsiteRepository.Remove(website);
                    UnitOfWork.Save();
                    TempData["Message"] = "Delete Website Successfully";

                    return RedirectToIndex();
                }
                catch
                {
                    TempData["ErrorMessage"] = "Delete Website Failed";
                }
            }

            return RedirectToIndex();
        }

        #endregion

        [ChildActionOnly]
        public ActionResult List()
        {
            return PartialView(WebsiteRepository.FindAll());
        }

        public string IdentifierAvailable(string identifier, int id)
        {
            return (!WebsiteRepository.FindAll().Any(i => i.Identifier.ToLower() == identifier.ToLower() && i.Id != id)).ToString().ToLower();
        }

        #region ToggleOnline
        public ActionResult ToggleOnline(int id)
        {
            var website = WebsiteRepository.FindAll().FirstOrDefault(i => i.Id == id);
            if (website == null)
            {
                TempData["ErrorMessage"] = "Website Not Exist";
            }
            else
            {
                try
                {
                    website.IsOnline = !website.IsOnline;
                    UnitOfWork.Save();
                    TempData["Message"] = "Toggle Website Successfully";

                    return RedirectToIndex();
                }
                catch
                {
                    TempData["ErrorMessage"] = "Toggle Website Failed";
                }
            }

            return RedirectToIndex();
        }

        #endregion
    }
}
